#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <cstdarg>
#include <ctime>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

enum Level
{
    Debug,
    Info, //消息
    Warning,
    Error,
    Fatal //致命
};

enum Style
{
    Screen,
    Onefile, //所有日志往一个文件写
    Classfile //日志分类写入文件
};

string levelToString(int level)
{
    switch(level)
    {
    case Debug:
        return "Debug";
    case Info:
        return "Info";
    case Warning:
        return "Warning";
    case Error:
        return "Error";
    case Fatal:
        return "Fatal";
    default:
        return "Unknow";
    }
}

const int defaultstyle = Screen;
const string defaultfilename = "log.";
const string logdir = "log";

class Log
{
public:
    Log():_style(defaultstyle),_filename(defaultfilename)
    {
        mkdir(logdir.c_str(),0775); //创建报存日志的目录
    }
    void Enable(int style)
    {
        _style = style;
    }
    string TimeStampTran()
    {
        time_t currtime = time(nullptr);
        struct tm *curr = localtime(&currtime);
        char time_buffer[128];
        snprintf(time_buffer, sizeof(time_buffer), "%d-%d-%d %d:%d:%d",
                 curr->tm_year + 1900, curr->tm_mon + 1, curr->tm_mday,
                 curr->tm_hour, curr->tm_min, curr->tm_sec);
        return time_buffer;
    }
    void WriteLogToOneFile(const string& logname,const string& message)
    {
        umask(0);
        int fd = open(logname.c_str(),O_WRONLY | O_APPEND | O_CREAT,0666);
        if(fd < 0) return;
        write(fd, message.c_str(), message.size());
        close(fd);
    }
    void WriteLogToClassfile(const string& levelstr,const string& message)
    {
        string logname = logdir;
        logname += '/';
        logname += _filename;
        logname += levelstr;
        WriteLogToOneFile(logname, message);
    }
    void WriteLog(const string& levelstr,const string& message)
    {
        switch(_style)
        {
        case Screen:
            cout << message << endl;
            break;
        case Onefile:
            WriteLogToClassfile("all",message);  //打印到.all文件
            break;
        case Classfile:
            WriteLogToClassfile(levelstr,message); //打印到对应.levelstr文件
            break;
        }
    }
    void LogMessage(int level,const char* format,...) //日志等级、日志内容(可变参数的形式)、日志时间
    {
        char content[1024];
        va_list args;
        va_start(args,format); //指向可变参数部分
        vsnprintf(content,sizeof(content),format,args);
        va_end(args);

        char leftbuff[1024];
        string levelstr = levelToString(level);
        string currtime = TimeStampTran();
        string currpid = to_string(getpid());
        snprintf(leftbuff,sizeof(leftbuff),"[%s][%s][%s]",levelstr.c_str(),currtime.c_str(),currpid.c_str());
        string loginfo = leftbuff;
        loginfo += content;
        WriteLog(levelstr,loginfo);
    }
    ~Log(){}
private:
    int _style; //打印类别(屏幕or文件)
    string _filename;
};

Log lg; //直接定义一个全局对象，外部直接使用

class Conf //配置类
{
public:
    Conf()
    {
        lg.Enable(Classfile);  //要改日志的输出形式直接改这里即可
    }
    ~Conf()
    {}
};

Conf conf; 